import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { config } from '../../../shared/smartadmin.config';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入

declare var $: any;
@Component({
	selector: 'sa-speedConfig',
	templateUrl: './speedConfig.component.html',
})
export class SpeedConfigComponent implements OnInit {
	validatorOptions = {
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			maxSpeed: {
				validators: {
					notEmpty: {
						message: '请输入超速速度'
					}
				}
			},
			timeNumber: {
				validators: {
					notEmpty: {
						message: '请输入超速时长'
					}
				}
			}

		}
	}
	maxSpeed:string='';//最大时速
	timeNumber:string=''; //持续时长
	speedConfigId:string = "";//速度配置数据ID
	
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {}

	ngOnInit() {
		this.jsonApiService.http("langYunConfigManage/configManage.html",'').subscribe(result => {
			if(result.success){
				this.speedConfigId = result.data.id?result.data.id:'';
				this.maxSpeed = result.data.speed;
				this.timeNumber = result.data.durationSecond;
			}
		})
		
	}
	//保存配置管理
	saveConfig(){
		$('#speedConfigForm').bootstrapValidator('validate'); //开启验证
		let isValid = $('#speedConfigForm').data('bootstrapValidator').isValid();  //验证是否通过
		if(isValid){
			let params={
				id:this.speedConfigId,
				speed:this.maxSpeed,
				durationSecond:this.timeNumber
			}
			let reqata='params='+JSON.stringify(params);
			this.jsonApiService.http("langYunConfigManage/updateConfigManage.html",reqata).subscribe(result => {
				this.jsonApiService.message(result.message);
			})
		}	
	}
	ngOnDestroy() {
		document.onclick = null;
	}

}